﻿using System;
using ParserGenerator.Exceptions;
using ParserGenerator.Extensions;
using ParserGenerator.Input;
using ParserGenerator.SyntacticAnalysis;

namespace ParserGenerator.ScannerConstruction
{
    [Serializable]
    public sealed class ScannerAutomatonStart
    {
        public int Number { get; internal set; }
        public string Name { get; internal set; }
        public SourceContext Context { get; internal set; }
        public bool IsExclusive { get; internal set; }
        public SemanticAction DefaultAction { get; private set; }
        public ScannerAutomatonState State { get; internal set; }
        public ScannerAutomatonState StrictState { get; internal set; }
        internal ScannerAutomatonItemSet Items { get; set; }
        internal ScannerAutomatonItemSet StrictItems { get; set; }

        internal ScannerAutomatonStart()
        {
            Items = new ScannerAutomatonItemSet();
            StrictItems = new ScannerAutomatonItemSet();
        }

        internal void SetDefaultAction(SemanticAction action)
        {
            if (action == null)
                throw new ArgumentNullException(nameof(action));
            if (DefaultAction != null)
            {
                string message = string.Format(SR.DefaultActionSpecifiedMoreThanOnce, Name.AsSingleQuote());
                GrammarErrorException e = new GrammarErrorException(message);
                e.AddLocation(DefaultAction.Context);
                e.AddLocation(action.Context);
                throw e;
            }
            DefaultAction = action;
        }

        public sealed override string ToString()
        {
            return Name;
        }
    }
}
